import 'package:flutter/material.dart';
import '../../../l10n/app_localizations.dart';

class CustomTextField extends StatefulWidget {
  final String hintText;
  final bool obscureText;
  final bool enabled;
  final TextEditingController? controller;
  final ValueChanged<String>? onChanged;
  final VoidCallback? onEditingComplete;
  final VoidCallback? onFocusLost;
  final List<String>? autofillHints;

  const CustomTextField({
    super.key,
    required this.hintText,
    this.obscureText = false,
    this.enabled = true,
    this.controller,
    this.onChanged,
    this.onEditingComplete,
    this.onFocusLost,
    this.autofillHints,
  });

  @override
  State<CustomTextField> createState() => _CustomTextFieldState();
}

class _CustomTextFieldState extends State<CustomTextField> {
  late TextEditingController _controller;
  late FocusNode _focusNode;
  bool _showPassword = false;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? TextEditingController();
    _focusNode = FocusNode();
    _focusNode.addListener(() {
      if (!_focusNode.hasFocus && widget.onFocusLost != null) {
        widget.onFocusLost!();
      }
    });
    _controller.addListener(() {
      setState(() {});
    });
  }

  void _togglePasswordVisibility() {
    setState(() {
      _showPassword = !_showPassword;
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      enableInteractiveSelection: true, // 必须为true

      controller: _controller,
      focusNode: _focusNode,
      obscureText: widget.obscureText && !_showPassword,
      enabled: widget.enabled,
      onChanged: widget.onChanged,
      onEditingComplete: widget.onEditingComplete,
      autofillHints: widget.autofillHints,
      decoration: InputDecoration(
        contentPadding: EdgeInsets.symmetric(horizontal: 18, vertical: 12),
        hintText: widget.hintText,
        hintStyle: TextStyle(color: Color(0xFFB2B2B2)),
        filled: true,
        fillColor: Colors.white,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(22),
          borderSide: BorderSide(color: Color(0xFFD2D2D2)),
        ),
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(22),
          borderSide: BorderSide(color: Color(0xFFD2D2D2)),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(22),
          borderSide: BorderSide(color: Color(0xFFD2D2D2)),
        ),
        suffixIcon: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (_controller.text.isNotEmpty)
              IconButton(
                padding: EdgeInsets.zero,
                icon: Image.asset(
                  'assets/images/close1.png',
                  width: 20,
                  height: 20,
                ),
                onPressed: () {
                  _controller.clear();
                  setState(() {});
                },
              ),
            if (widget.obscureText)
              IconButton(
                padding: EdgeInsets.zero,
                icon: Image.asset(
                  _showPassword ? 'assets/images/eyeselect.png' : 'assets/images/eye.png',
                  width: 30,
                  height: 30,
                ),
                onPressed: _togglePasswordVisibility,
              ),
          ],
        ),
      ),
      style: TextStyle(color: Color(0xFF222222),fontSize: isChineseLocale() ? 14 : 13),
    );
  }
}